package com.newangels.action;

import jade.util.leap.ArrayList;
import jade.util.leap.List;
import jade.wrapper.ControllerException;
import jade.wrapper.gateway.JadeGateway;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * 关闭容器  初始化MyGatewayAgent智能体
 *
 * @author: TangLiang
 * @date: 2020/5/16 17:21
 * @since: 1.0
 */
@WebServlet("/killAgent")
public class KillAgentServlet extends HttpServlet {
    private static Logger logger = Logger.getLogger(KillAgentServlet.class.getName());

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        String[] agentList = request.getParameterValues("AGENT_LIST");
        List list = new ArrayList();
        for (int i = 0, len = agentList.length; i < len; i++) {
            list.add(i, agentList[i]);
        }
        try {
            JadeGateway.execute(list);
        } catch (ControllerException e) {
            logger.warning(e.getMessage());
            e.printStackTrace();
        } catch (InterruptedException e) {
            logger.warning(e.getMessage());
            e.printStackTrace();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        response.getWriter().write(jsonObject.toString());
    }
}
